using System;
using System.Collections.Generic;
using System.Data;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
using Anurgath.Shoutbox.Models;

namespace Anurgath.Shoutbox {
    public class Migrations : DataMigrationImpl {

        public int Create() {
            // Creating table PostPartRecord
            SchemaBuilder.CreateTable("PostPartRecord", table => table
                .ContentPartRecord()
                .Column("UserId", DbType.Int32)
                .Column("Content", DbType.String)
                .Column("AddDate", DbType.DateTime2)
                .Column("ParentShoutbox_id", DbType.Int32)
            );

            // Creating table ShoutboxPartRecord
            SchemaBuilder.CreateTable("ShoutboxPartRecord", table => table
                .ContentPartRecord()
                .Column("CreatedAt", DbType.DateTime2)
                .Column("HowManyPostsToShow", DbType.Int32)
            );

            ContentDefinitionManager.AlterPartDefinition(typeof(ShoutboxPart).Name, g => g.Attachable());
            ContentDefinitionManager.AlterTypeDefinition("ShoutboxWidget", type => type
                .WithPart(typeof(ShoutboxPart).Name)
                .WithPart("WidgetPart")
                .WithPart("CommonPart")
                .WithSetting("Stereotype", "Widget")
            );

            ContentDefinitionManager.AlterPartDefinition(typeof(PostPart).Name, g => g.Attachable());
            ContentDefinitionManager.AlterTypeDefinition("ShoutboxPost", type => type.WithPart(typeof(PostPart).Name));

            return 1;
        }
    }
}